package com.example.search.service;
import com.alibaba.dubbo.config.annotation.Service;
import com.example.service.RpcDoctorSearchService;
import com.example.vo.SearchDoctorInfo;
import com.example.vo.SearchDoctorReturnVo;
import com.example.vo.SearchDoctorVO;
import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.client.solrj.SolrServerException;
import org.apache.solr.client.solrj.impl.HttpSolrClient;
import org.apache.solr.client.solrj.impl.XMLResponseParser;
import org.apache.solr.client.solrj.response.QueryResponse;
import org.apache.solr.common.SolrDocumentList;
import org.springframework.stereotype.Component;
import java.io.IOException;
import java.util.List;
/**
 * Created by LENOVO on 2018/9/21.
 */
@Component
@Service(interfaceClass =RpcDoctorSearchService.class)
public class RpcDoctorSearchServiceImpl implements RpcDoctorSearchService {

    /**
     * solr数据路径
     */
    public static String url = "http://127.0.0.1:8081/solr/doctor/";
    /**
     * 初始化httpSolrClient
     */
    HttpSolrClient httpSolrClient = new HttpSolrClient(url);
    /**
     * 查询数量
     */
    private int total=0;
    @Override
    public SearchDoctorReturnVo getDoctorList(SearchDoctorVO searchDoctorVO) throws Exception {
        httpSolrClient.setParser(new XMLResponseParser());
        httpSolrClient.setConnectionTimeout(500);

        List<SearchDoctorInfo> list= null;
        try {
            list = this.search(searchDoctorVO);
        } catch (IOException e) {
            e.printStackTrace();
        } catch (SolrServerException e) {
            e.printStackTrace();
        }
        SearchDoctorReturnVo searchDoctorReturnVo = new SearchDoctorReturnVo();
        searchDoctorReturnVo.setRows(list);
        return searchDoctorReturnVo;
    }

    /**
     * 进行医生信息查询
     * @param searchDoctorVO
     * @return
     */
    private List<SearchDoctorInfo> search(SearchDoctorVO searchDoctorVO) throws IOException, SolrServerException {

        SolrQuery query = new SolrQuery("*:*");
        if(searchDoctorVO!=null)
        {
            StringBuilder sb=new StringBuilder();

            if(searchDoctorVO.getDepartmentleverl1()!=null&&searchDoctorVO.getDepartmentleverl1()!=0)
            {
                query.addFilterQuery("departmentLeverl1:"+searchDoctorVO.getDepartmentleverl1());
            }
            if(searchDoctorVO.getDepartmentleverl2()!=null&&searchDoctorVO.getDepartmentleverl2()!=0)
            {
                query.addFilterQuery("departmentLeverl2:"+searchDoctorVO.getDepartmentleverl2());
            }
            if (searchDoctorVO.getServiceid()!=null&&searchDoctorVO.getServiceid().length()!=0&&!searchDoctorVO.getServiceid().equals("string")) {
                StringBuilder featureIdBuff = new StringBuilder("(");
                featureIdBuff.append("serviceNameIds:" + "*," + searchDoctorVO.getServiceid() + ",*");
                featureIdBuff.append(")");
                query.addFilterQuery(featureIdBuff.toString());
            }
            if (searchDoctorVO.getDescSort()!= null && searchDoctorVO.getDescSort().length()!=0&&!searchDoctorVO.getDescSort().equals("string")) {
                query.addSort(searchDoctorVO.getDescSort(), SolrQuery.ORDER.desc);
            }
        }
        QueryResponse queryResponse=httpSolrClient.query(query);
        SolrDocumentList docs = queryResponse.getResults();
        total=new Long(docs.getNumFound()).intValue();
        return queryResponse.getBeans(SearchDoctorInfo.class);
    }
}
